package oneD.test;

import inf.jlinalg.*;

public class BLAMExample {
	
	private static void example1() { //multiply matrix -> ok
		IMatrix a = new Array2DMatrix(2, 3);
		IMatrix b = new Array2DMatrix(2, 2);
		IMatrix tmp = new Array2DMatrix(3, 2);
		IMatrix c = new Array2DMatrix(3, 3);
		// initialize A and B
		a.set(0, 0, 1);
		a.set(0, 1, -6);
		a.set(0, 2, 3);
		a.set(1, 0, -4);
		a.set(1, 1, 2);
		a.set(1, 2, 5);
		b.set(0, 0, -1);
		b.set(0, 1, 3);
		b.set(1, 0, 9);
		b.set(1, 1, 2);
		// compute
		BLAM.multiply(1.0, BLAM.TRANSPOSE, a, BLAM.NO_TRANSPOSE, b, 0.0, tmp);
		BLAM.multiply(1.0, BLAM.NO_TRANSPOSE, tmp, BLAM.NO_TRANSPOSE, a, 0.0, c);
		// print
		System.out.println(" Matrix A");
		System.out.println(MatrixFormat.format(a));
		System.out.println(" Matrix B");
		System.out.println(MatrixFormat.format(b));
		System.out.println(" Matrix C = A^T B A");
		System.out.println(MatrixFormat.format(c));
	}
	
	private static void example2() { //multiply matrix by vector -> ok
		IMatrix A = new Array2DMatrix(3, 3);
		IVector v = new ArrayVector(3);
		A.set(0, 0, 1);
		A.set(0, 1, -1);
		A.set(0, 2, 2);
		A.set(1, 0, 0);
		A.set(1, 1, 2);
		A.set(1, 2, 0);
		A.set(2, 0, -1);
		A.set(2, 1, 1);
		A.set(2, 2, 3);
		v.set(0, 1);
		v.set(1, 1);
		v.set(2, 1);
		BLAM.multiply(1, BLAM.NO_TRANSPOSE, A, v, 0, v);
		System.out.println(MatrixFormat.format(v));
	}
	
	private static void example3() {//add 2 vectors -> error
		IVector a = new ArrayVector(3);
		IVector b = new ArrayVector(3);
		a.set(0, 1);
		a.set(1, 2);
		a.set(2, 3);
		b.set(0, 1);
		b.set(1, 1);
		b.set(2, 1);
		BLAM.add(1, a, b);
		System.out.println(MatrixFormat.format(b));
	}
	
	private static void example4() {//add 2 matrices -> ok
		IMatrix A = new Array2DMatrix(3, 3);
		IMatrix B = new Array2DMatrix(3, 3);
		A.set(0, 0, 1);
		A.set(0, 1, -1);
		A.set(0, 2, 2);
		A.set(1, 0, 0);
		A.set(1, 1, 2);
		A.set(1, 2, 0);
		A.set(2, 0, -1);
		A.set(2, 1, 1);
		A.set(2, 2, 3);

		B.set(0, 0, 1);
		B.set(0, 1, -1);
		B.set(0, 2, 2);
		B.set(1, 0, 0);
		B.set(1, 1, 2);
		B.set(1, 2, 0);
		B.set(2, 0, -1);
		B.set(2, 1, 1);
		B.set(2, 2, 3);
		
		BLAM.add(1, A, B);
		System.out.println(MatrixFormat.format(B));
	}
	
	private static void example5() {//scale a vector -> error
		IVector a = new ArrayVector(3);
		a.set(0, 1);
		a.set(1, 2);
		a.set(2, 3);
		BLAM.scale(0.5, a);
		System.out.println(MatrixFormat.format(a));
	}
	
	private static void example6() {//scale a matrix -> ok
		IMatrix A = new Array2DMatrix(3, 3);
		A.set(0, 0, 1);
		A.set(0, 1, -1);
		A.set(0, 2, 2);
		A.set(1, 0, 0);
		A.set(1, 1, 2);
		A.set(1, 2, 0);
		A.set(2, 0, -1);
		A.set(2, 1, 1);
		A.set(2, 2, 3);
		BLAM.scale(0.5, A);
		System.out.println(MatrixFormat.format(A));
	}
	
	public static void main(String[] args) {
		example6();
	}
}
